using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxMenu;
using Indeco.G24.Data;
using Indeco.G24.Services;
using Indeco.G24.Data.SqlClient;
using Indeco.G24.Entities;
using System.Windows.Forms;
using System.Xml;
using System.Net;

namespace Indeco.G24.Website.News
{
    public partial class RSS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string idDanhMucCha =Request.QueryString["idDanhMuc"];
            if (!string.IsNullOrEmpty(idDanhMucCha))
            {
                Response.Clear();
                Response.ContentType = "text/xml";
                XmlTextWriter xtw = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
                xtw.WriteStartDocument();
                TinTucService tintucService = new TinTucService();
                TinTucDanhMucService danhmucService = new TinTucDanhMucService();
                TList<TinTucDanhMuc> danhmuc = danhmucService.Find("id_tintucdanhmuc='" + idDanhMucCha + "'");
                if (danhmuc != null && danhmuc.Count > 0)
                {
                    int outcount;
                    string path = "..\\Data\\Files\\rss.xsl";
                    string domain = "http://" + Request.Url.Host + ":" + Request.Url.Port + Request.ApplicationPath;
                    string processtext = "type=\"text/xsl\" href=\"" + path + "\"";
                    xtw.WriteProcessingInstruction("xml-stylesheet", processtext);
                    xtw.WriteStartElement("rss");
                    xtw.WriteAttributeString("version", "2.0");
                    xtw.WriteStartElement("channel");
                    xtw.WriteElementString("title", danhmuc[0].Ten.ToString()+" - Gia24.com");
                    xtw.WriteElementString("link",domain+"/News/Category.aspx?IDTinTucDanhMuc=" +idDanhMucCha);
                    xtw.WriteElementString("description", "Chung Ta Cung Xay Dung");
                    TList<TinTuc> newsTop = tintucService.GetPaged("nhacungcap is null and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_tintucdanhmuc where id_tintucdanhmuccha ='" + idDanhMucCha + "')", "thoidiemcapnhat DESC", 0, 10, out outcount);
                    if (newsTop != null && newsTop.Count > 0)
                    {
                        foreach (TinTuc tin in newsTop)
                        {
                            xtw.WriteStartElement("item");
                            xtw.WriteElementString("title", tin.TieuDe.ToString());
                            xtw.WriteElementString("description", tin.MoTa.ToString());
                            xtw.WriteElementString("link","http://" + Request.Url.Host + ":" + Request.Url.Port + Request.ApplicationPath+"News/Category.aspx?IDTinTucDanhMuc=" + tin.IDTinTucDanhMuc.ToString() + "&IDTinTuc=" + tin.IDTinTuc.ToString());
                            xtw.WriteElementString("pubDate", XmlConvert.ToString(Convert.ToDateTime(tin.ThoiDiemCapNhat.ToString())));
                            xtw.WriteEndElement();
                        }
                    }
                    xtw.WriteEndElement();
                    xtw.WriteEndElement();
                    xtw.WriteEndDocument();

                    xtw.Flush();
                    xtw.Close();
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = "text/xml";
                    Response.Cache.SetCacheability(HttpCacheability.Public);
                    Response.End();
                }
            }
        }
    }
}
